package com.dna.controller;

import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.dna.entity.DictEntity;
import com.dna.service.DictService;
import com.dna.utils.R;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping("/dict")
@Api("字典信息")
public class DictController {

    @Autowired
    private DictService dictService;

    @GetMapping("list")
    @ApiOperation(value = "查询所有字典")
    public R list() {
        List<DictEntity> dictlist = dictService.queryAll();
        return R.ok().put("data", dictlist);

    }

    @PostMapping
    @ApiOperation(value = "保存字典")
    public R saveDict(DictEntity dict) {

        DictEntity dictEntity = dictService.queryOne(dict);
        if (dictEntity != null) {
            return R.error(dict.getName() + " 已存在!");
        }
        dictService.save(dict);
        return R.ok();
    }

    @PutMapping
    @ApiOperation(value = "更新字典")
    public R updateDict(DictEntity dict) {
        dictService.update(dict);
        return R.ok();
    }

    @DeleteMapping
    @ApiOperation(value = "删除字典")
    public R deleteDict(@RequestBody Set<Long> ids) {
        dictService.delete(ids);
        return R.ok();
    }
}